package exam;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Tests the answer to the third map question.
 * The text of the question is as follows:
 * "Given a String, return a Map<Character, Integer> containing the frequency of each 
 *  character in the String."
 * 
 * @author Branden Ogata
 *
 */

public class TestMapA02 extends Tester
{
  public static void main(String[] args)
  {
    final double POINTS = getPoints(args);

    MapA02 question = new MapA02();
    Map<Character, Integer> answers = new HashMap<Character, Integer>();
    
    System.out.println(test(Category.EQUALS, "countChars(\"\")", 
                            answers, question.countChars(""), 
                            percentageToPoints(FOURTH, POINTS)));    
    
    answers.put('Z', 1);
    System.out.println(test(Category.EQUALS, "countChars(\"Z\")", 
                            answers, question.countChars("Z"), 
                            percentageToPoints(FOURTH, POINTS)));    
    answers.clear();

    String testString = "124816326412825651210242048409681921638432768";
    
    answers.put('0', 3);
    answers.put('1', 7);
    answers.put('2', 9);
    answers.put('3', 3);
    answers.put('4', 6);
    answers.put('5', 2);
    answers.put('6', 6);
    answers.put('7', 1);
    answers.put('8', 6);
    answers.put('9', 2);
    
    System.out.println(test(Category.EQUALS, "countChars(\"" + testString + "\")", 
        answers, question.countChars(testString), 
        percentageToPoints(FOURTH, POINTS)));    
    answers.clear();

    try
    {
      testString = "";
      StringBuffer buffer = new StringBuffer();
      
      Scanner input = new Scanner(new File("exam/syllabus.txt"));
      
      while (input.hasNextLine())
      {
        String line = input.nextLine();
        buffer.append(line);
        
        for (char c : line.toCharArray())
        {
          answers.put(c, ((answers.containsKey(c)) ? (answers.get(c) + 1) : (1)));
        }
      }
      
      testString = buffer.toString();
      
      System.out.println(test(Category.EQUALS, "countChars(syllabus)", 
                              answers, question.countChars(testString), 
                              percentageToPoints(FOURTH, POINTS)));    
      answers.clear();                 
    }
    catch (IOException e)
    {
      System.out.println(e.getMessage());
    }
    
    System.out.format("[%.2f/%.2f]%n", getTotalPoints(), POINTS);
  }
}
